package org.dromara.demo.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseService;
import org.dromara.demo.domain.DemoExample5;
import org.dromara.demo.domain.DemoExample5Line;
import org.dromara.demo.service.DemoExample5ServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/demo/example5")
public class DemoExample5Controller extends BaseService {

  private final DemoExample5ServiceImpl testDemo5Service;


  /**
   * 查询测试主从列表5
   */
  @SaCheckPermission("demo:example5:list")
  @GetMapping("/list")
  public TableDataInfo<DemoExample5> list(DemoExample5 bo, PageQuery pageQuery) {
    return testDemo5Service.queryPageList(bo, pageQuery);
  }


  /**
   * 导出测试主从列表
   */
  @SaCheckPermission("demo:example5:export")
  @Log(title = "测试主从", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(DemoExample5 bo, HttpServletResponse response) {
    List<DemoExample5> list = testDemo5Service.queryList(bo);
    ExcelUtil.exportExcel(list, "测试主从", DemoExample5.class, response);
  }


  /**
   * 获取测试主从详细信息
   * @param id 主键
   */
  @SaCheckPermission("demo:example5:query")
  @GetMapping("/{id}")
  public R<DemoExample5> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
    return R.ok(testDemo5Service.queryById(id));
  }


  /**
   * 新增测试主从
   */
  @SaCheckPermission("demo:example5:add")
  @Log(title = "测试主从", businessType = BusinessType.INSERT)
  @RepeatSubmit()
  @PostMapping()
  public R<Void> add(@Validated(AddGroup.class) @RequestBody DemoExample5 bo) {
    return toAjax(testDemo5Service.insertByBo(bo));
  }


  /**
   * 修改测试主从
   */
  @SaCheckPermission("demo:example5:edit")
  @Log(title = "测试主从", businessType = BusinessType.UPDATE)
  @RepeatSubmit()
  @PutMapping()
  public R<Void> edit(@Validated(EditGroup.class) @RequestBody DemoExample5 bo) {
    return toAjax(testDemo5Service.updateByBo(bo));
  }

  /**
   * 删除测试主从
   *
   * @param ids 主键串
   */
  @SaCheckPermission("demo:example5:remove")
  @Log(title = "测试主从", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
    return toAjax(testDemo5Service.deleteWithValidByIds(List.of(ids), true));
  }

  /**
   * 查询测试从列表======================Line表的路由服务==========================
   */
  @SaCheckPermission("demo:example5:list")
  @GetMapping("/lineList")
  public TableDataInfo<DemoExample5Line> lineList(DemoExample5Line bo, PageQuery pageQuery) {
    return testDemo5Service.queryLinePageList(bo, pageQuery);
  }


  /**
   * 删除测试从
   * @param lineIds 主键串
   */
  @SaCheckPermission("demo:example5:remove")
  @Log(title = "测试从", businessType = BusinessType.DELETE)
  @DeleteMapping("/lineId/{lineIds}")
  public R<Void> removeLine(@NotEmpty(message = "主键不能为空") @PathVariable Long[] lineIds) {
    return toAjax(testDemo5Service.deleteWithValidByLineIds(List.of(lineIds), true));
  }
}
